package cn.sc.summer.rabbitmq.service.impl;

import cn.sc.summer.rabbitmq.enums.RabbitErrorMessageTypeEnum;
import com.alibaba.fastjson.JSONObject;
import cn.sc.summer.constant.rabbit.RabbitConstant;
import cn.sc.summer.rabbitmq.builder.RabbitBuilder;
import cn.sc.summer.rabbitmq.builder.RabbitMessageBuilderDirector;
import cn.sc.summer.rabbitmq.service.RabbitErrorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 类名：消费者异常信息转发接口
 *
 * @author a-xin
 * @date 2024/1/29 11:25
 */
@Slf4j
@Service
public class RabbitErrorServiceImpl implements RabbitErrorService {

    @Resource
    private RabbitTemplate rabbitTemplate;

    /**
     * 将消费者异常信息转发
     *
     * @param errorMessage               报错信息
     * @param params                     处理的参数信息
     * @param rabbitErrorMessageTypeEnum 处理失败枚举类
     */
    @Override
    public void exchangeMessage(String errorMessage, String params, RabbitErrorMessageTypeEnum rabbitErrorMessageTypeEnum) {
        JSONObject rabbitErrorMessage = new RabbitMessageBuilderDirector(new RabbitBuilder()).build();
        Map<String, Object> message = new HashMap<>();
        message.put("errorMessage", errorMessage);
        message.put("params", params);
        message.put("errorType", rabbitErrorMessageTypeEnum.getType());
        message.put("errorDesc", rabbitErrorMessageTypeEnum.getInfo());
        rabbitErrorMessage.put(RabbitConstant.MESSAGE_KEY, JSONObject.toJSONString(message));
        rabbitTemplate.convertAndSend(RabbitConstant.ERROR_RECORD_EXCHANGE, RabbitConstant.ERROR_RECORD_ROUTING_KEY, rabbitErrorMessage.toJSONString());
    }

}
